package rickyGame.server.state
{
	import flash.geom.Point;
	
	import ricky.utils.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	
	import rickyGame.controller.state.State;
	import rickyGame.game.Fighter;
	import rickyGame.game.action.ActionHelper;
	import rickyGame.game.action.MoveLeafAction;
	import rickyGame.game.action.ai.AutoAttackAi;
	import rickyGame.game.action.ai.AutoAttackTarget;
	
	/**
	 * 长期ai决策层
	 * 负责一些决策层面的ai行动，比如派新出生的兵攻击对方</br>
	 * 军队的ai目标
	 * 1：军队被创建出来后，移动到敌方粮草位置。 需要的行动(moveLeafAct)
	 * 2：行军途中遇到被攻击要还击对方，直到对方死亡或者自己死亡。(attack)
	 * 3：如果敌人死亡，继续移动到粮草位置。
	 * 3：如果顺利到达粮草位置，攻击粮草。
	 * 
	 */
	public class LongDecisionAi extends Act2D
	{
		private static var _inst:LongDecisionAi;
		
		public static function get inst():LongDecisionAi
		{
			if(!_inst)
				_inst=new LongDecisionAi();
			return _inst;
		}
		
		public function LongDecisionAi()
		{
			super(null);
			_army=[];
			ActionHelper.inst.addAct(this);
		}
	
		override public function begin():void
		{
			_last=TimeServer.now();
			_inteval=1200;
		}
		
		override public function action(timeline:Number):void
		{
			var dt:Number=timeline-_last;
			if(dt>_inteval)
			{
				_last=timeline;
				var fighter:Fighter,act:Act2D;
				for each(fighter in _army)
				{
					if(fighter.isDead)
						quitTheArmy(fighter);
					
					act=Act2D(fighter.act);
					fighter.name=fighter.data.name+" "+act.type;
					if(act.type=="IdleAi")//如果是闲着就移动到目标
					{
						if(isArrive(fighter))
						{
							if(act.type!="AutoAttackAi")
							{
								fighter.addAct(new AutoAttackAi(fighter));
							}
						}else
							moveAct(fighter);
					}
					else if(act.type=="MoveLeafAction") //如果在移动
					{
						if(dt % 50==0)
						{
							if(fighter.ai.hurtMe)
							{
								fighter.addAct(new AutoAttackTarget(fighter,fighter.ai.hurtMe));//反击
							}
						}
					}
				}
			}
		}
		
		//************************************
		//
		//************************************
		
		private function isArrive(fighter:Fighter):Boolean
		{
			var group:int=fighter.group;
			var target:Actor;//
			if(group==0)
				target=this._longg2Target
			else if(group==1)
				target=this._longg1Target;
			
			var _calcPoint:Point=new Point(target.graphic.x,target.graphic.y);
			var tarP:Point=new Point(fighter.graphic.x,fighter.graphic.y);
			if(tarP.subtract(_calcPoint).length<40)//if(x==this._currentObject.coordinate.x && this._currentObject.coordinate.y==y)
				return true;
			else
				return false;
		}
		
		/**
		 * 移动到目标
		 */
		private function moveAct(fighter:Fighter):void
		{
			var group:int=fighter.group;
			var target:Actor;//
			if(group==0)
				target=this._longg2Target
			else if(group==1)
				target=this._longg1Target;
			
//			fighter.ai.

			var p:Point=new Point(target.graphic.x,target.graphic.y);
			var path:Array=State.pathFinder(p,fighter);
			if(path)
			{
				var mv:MoveLeafAction=new MoveLeafAction(fighter,null,path);
//				mv.addNext(msg);
				if(fighter.isLock==true)
					fighter.isLock=false;

				fighter.addAct(mv);
			}
		}
		
		//***********************************************************
		//
		//***********************************************************
		/**
		 * 入伍
		 */
		public function enterTheArmy(figher:Fighter):Boolean
		{
			var i:int=_army.indexOf(figher);
			if(i==-1)
			{
				_army.push(figher);
				return true;
			}
			return false;
		}
		
		/**
		 * 退伍
		 */
		public function quitTheArmy(figher:Fighter):Boolean
		{
			var i:int=_army.indexOf(figher);
			if(i>0)
			{
				_army.splice(i,1);
				return true;
			}
			return false;
		}

		public function get longg1Target():Actor
		{
			return _longg1Target;
		}

		public function set longg1Target(value:Actor):void
		{
			_longg1Target = value;
		}

		public function get longg2Target():Actor
		{
			return _longg2Target;
		}

		public function set longg2Target(value:Actor):void
		{
			_longg2Target = value;
		}


		
		//
		private var _army:Array;//军队
		private var _longg1Target:Actor;//长期目标
		private var _longg2Target:Actor;//长期目标

		//
		private var _inteval:Number;//间隔
		private var _last:Number;//上次时间
		
	}
}